import {getShopcartList,changeCartChecked,changeAllChecked,deleteOneCart,deleteCheckedCart} from '@/ajax'
const state = {
  shopCartList: []
}
const actions = {
  async getShopcartList({commit}) {
    let {data,code} = await getShopcartList()
    if(code === 200) {
      commit('GETSHOPCARTLIST',data)
    }
  },
  async changeCartChecked({commit},{skuID,isChecked}) {
    let result = await changeCartChecked(skuID,isChecked)
      if(result.code === 200) {
        return 'ok'
      }else {
        return Promise.reject(new Error('修改商品选中状态失败'))
      }
  },
  async changeAllChecked({commit},{isChecked,skuIds}) {
    let result = await changeAllChecked(isChecked,skuIds)
    if(result.code === 200) {
      return 'ok'
    }else {
      return Promise.reject(new Error('批量更改选中状态失败'))
    }
  },
  async deleteOneCart({commit},skuId) {
    let result = await deleteOneCart(skuId)
    if(result.code === 200) {
      return 'ok'
    }else {
      return Promise.reject(new Error('删除单个商品失败'))
    }
  },
  async deleteCheckedCart({commit},skuIds) {
    let result = await deleteCheckedCart(skuIds)
    if(result.code === 200) {
      return 'ok'
    }else {
      return Promise.reject(new Error('删除已选中商品失败'))
    }
  }
}
const mutations = {
  GETSHOPCARTLIST(state,data) {
    state.shopCartList = data
  }
}
const getters = {
  
}
export default {
  state,
  actions,
  mutations,
  getters
}